﻿using Tao.OpenGl;

using System;
using VectorMath;

namespace Candle
{
    /** 
     * Simple object for setting up orthographic projection in OpenGL, and
     * mapping mouse clicks into the unit computational cell.
     * 
     * @author Doug James, January 2007
     */
    public class OrthoMap
    {
        private double eps = 0.03;//epsilon boundary gap
        private int width, height;
        private double r;
        private double L = 0;
        private double R;//   = r*1;
        private double B = 0;
        private double T = 8;

        public OrthoMap(int viewportWidth, int viewportHeight)
        {
            width = viewportWidth;
            height = viewportHeight;
            r = 1; // (double)width / (double)height;
            //L   = B = 0;	  
            R = r * 8;
            //T   = 1;
            Console.Out.WriteLine("r=" + r);
        }

        public void apply_glOrtho()
        {
            Gl.glOrtho(L - eps, R + eps, B - eps, T + eps, -8 + eps, +8 + eps);
        }

        /** Get 2d coordinates in unit computation cell of e using
         * knowledge of ortho projection. */
        public Pnt2D getPnt2D(System.Windows.Forms.MouseEventArgs e)
        {
            System.Drawing.Size size = TaoForm.ActiveForm.ClientRectangle.Size;

            double x = (double)e.X / (double)size.Width;      /// on [0,1] (unless outside mouse click)
            x *= (r + 2 * eps);
            x -= eps;

            double y = 1.0 - (double)e.Y / (double)size.Height;/// on [0,1] (unless outside mouse click)
            y *= (1 + 2 * eps);
            y -= eps;

            Pnt2D p = new Pnt2D();
            p.x = x;
            p.y = y;
            // 	    p.clampMax(1);
            // 	    p.clampMin(0);
            //Console.Out.WriteLine(p);
            return p;
        }


    }
}